package com.example.myapplication11_10;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LineChart linechart =findViewById(R.id.lineChart);
        //线颜色


        float[] f1 ={4.0f,5.0f,5.0f,8.0f,6.0f,4.0f,6.0f,7.0f};
        float [] f2={6.0f,8.0f,7.0f,4.0f,5.0f,8.0f,4.0f,5.0f};


        List<ILineDataSet> s=new ArrayList<>();
        LineDataSet demo1=getDataSet(f1,"demo1");
        demo1.setColor(Color.parseColor("#ff0000"));

        demo1.setLineWidth(1.6f);
        demo1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        s.add(demo1);

        LineDataSet demo2=getDataSet(f2,"demo2");
        demo2.setColor(Color.parseColor("#00ff00"));
        demo2.setLineWidth(3.2f);
        demo2.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        s.add(demo2);

        XAxis xAxis=linechart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
        xAxis.setDrawAxisLine(true);


        linechart.setData(new LineData(s));
        linechart.invalidate();

    }

    private LineDataSet getDataSet(float[]f,String title) {
        List<Entry> data = new ArrayList<>();
        for (int x = 0; x < f.length; x++) {
            Entry entry = new Entry(x, f[x]);
            data.add(entry);

        }

        return new LineDataSet(data, title);


    }
}

